package ch.supsi.template.money.view;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MoneyViewer {
    public static void main(String[] args) {
        JFrame moneyViewer = new JFrame("Money");
        attachPanes(moneyViewer);
        moneyViewer.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        moneyViewer.setVisible(true);
        moneyViewer.pack();
    }

    private static void attachPanes(JFrame moneyViewer) {
        final JTextField firstOperand = new JTextField();
        final JTextField secondOperand = new JTextField();
        final JTextField result = new JTextField();
        result.setEditable(false);
        JButton addOperation = new JButton("ADD");

        JPanel gridPanel = new JPanel();
        gridPanel.setLayout(new GridLayout(2, 2));
        gridPanel.add(firstOperand);
        gridPanel.add(secondOperand);
        gridPanel.add(addOperation);

        moneyViewer.getContentPane().setLayout(new BorderLayout());
        moneyViewer.getContentPane().add(BorderLayout.NORTH, gridPanel);
        moneyViewer.getContentPane().add(BorderLayout.SOUTH, result);


        addOperation.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                int first;
                int second;
                try {
                    first = Integer.parseInt(firstOperand.getText());
                    second = Integer.parseInt(secondOperand.getText());

                    result.setText(String.valueOf(first + second));
                } catch (NumberFormatException e) {
                    result.setText(String.valueOf(0));
                }

            }
        });
    }
}
